package com.markus.code.图.图2;

import java.util.HashMap;

/**
 * Author:markusZhang
 * Date:Create in 2020/8/30 15:24
 * todo:
 */
public class GraphGenerator {
    public static Graph generator(int [][]matrix){
        Graph graph = new Graph();
        HashMap<Integer,Node> nodeMap = new HashMap<>();
        for (int i=0;i<matrix.length;i++){
            int weight = matrix[i][0];
            int fromValue = matrix[i][1];
            int toValue = matrix[i][2];
            if (!nodeMap.containsKey(fromValue)){
                nodeMap.put(fromValue,new Node(fromValue));
            }
            if (!nodeMap.containsKey(toValue)){
                nodeMap.put(toValue,new Node(toValue));
            }
            Node fromNode = nodeMap.get(fromValue);
            Node toNode = nodeMap.get(toValue);
            Edge edge = new Edge(fromNode,toNode,weight);
            fromNode.edges.add(edge);
            fromNode.out++;
            fromNode.nextNodes.add(toNode);
            toNode.in++;
            graph.nodes.put(fromValue,fromNode);
            graph.nodes.put(toValue,toNode);
            graph.edges.add(edge);
        }
        return graph;
    }
}
